package com.qiaoba.auth.runner;

import cn.hutool.core.util.RandomUtil;
import com.qiaoba.api.auth.constant.SecurityConstant;
import com.qiaoba.api.auth.utils.JwtUtil;
import com.qiaoba.nosql.redis.service.RedisService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 项目启动后生成Jwt密钥
 *
 * @author ailanyin
 * @date 2023-09-17 13:00:41
 */
@Component
@RequiredArgsConstructor
public class JwtSecretRunner implements ApplicationRunner {

    private final RedisService redisService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Object secret = redisService.get(SecurityConstant.JWT_SECRET_KEY);
        if (Objects.isNull(secret)) {
            secret = RandomUtil.randomString(32);
            redisService.set(SecurityConstant.JWT_SECRET_KEY, secret);
        }
        JwtUtil.setSecretKey(secret.toString());
    }
}
